package com.iraeta.service.gift.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iraeta.common.ServiceException;
import com.iraeta.entity.SysOrg;
import com.iraeta.entity.TreeNode;
import com.iraeta.entity.gift.Gift;
import com.iraeta.entity.gift.GiftType;
import com.iraeta.entity.vo.GiftTypeTreeNode;
import com.iraeta.mapper.gift.GiftMapper;
import com.iraeta.mapper.gift.GiftTypeMapper;
import com.iraeta.service.gift.IGiftTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 礼品分类表 服务实现类
 * </p>
 *
 * @author chenbin
 * @since 2023-12-20
 */
@Service
public class GiftTypeServiceImpl extends ServiceImpl<GiftTypeMapper, GiftType> implements IGiftTypeService {
    @Resource
    GiftMapper giftMapper;

    @Override
    public List<GiftTypeTreeNode> getGiftTypeTreeNode() {
        List<GiftType> giftTypeList = baseMapper.selectList(new QueryWrapper<GiftType>());
        List<GiftType> rootGiftTypeList  = giftTypeList.stream().filter(x->x.getPid()==0).collect(Collectors.toList());
        ArrayList<GiftTypeTreeNode> res = new ArrayList<>();
        for(GiftType giftType : rootGiftTypeList){
            GiftTypeTreeNode giftTypeTreeNode = new GiftTypeTreeNode();
            BeanUtil.copyProperties(giftType, giftTypeTreeNode);
            getChildrenGiftTypeLoop(giftTypeTreeNode, giftTypeList);
            res.add(giftTypeTreeNode);
        }
        return res;
    }
    private void getChildrenGiftTypeLoop(GiftTypeTreeNode treeNode, List<GiftType> giftTypeTreeNodeList){
        String idStr = String.valueOf(treeNode.getId());
        List<GiftType> childGiftTypeList = giftTypeTreeNodeList.stream().filter(x-> x.getPid().toString().equals(idStr)).collect(Collectors.toList());
        for(GiftType childNode : childGiftTypeList){
            GiftTypeTreeNode childTreeNode = new GiftTypeTreeNode();
            childTreeNode.setId(childNode.getId());
            childTreeNode.setName(childNode.getName());
            treeNode.addNode(childTreeNode);
            getChildrenGiftTypeLoop(childTreeNode, giftTypeTreeNodeList);
        }
    }


    @Override
    public List<TreeNode> getTreeNode() {
        List<GiftType> giftTypeList = baseMapper.selectList(new QueryWrapper<GiftType>());
        List<GiftType> rootGiftTypeList  = giftTypeList.stream().filter(x->x.getPid()==0).collect(Collectors.toList());
        ArrayList<TreeNode> res = new ArrayList<>();
        for(GiftType giftType : rootGiftTypeList){
            TreeNode treeNode = new TreeNode(giftType.getId().toString(), giftType.getName());
            getChildTreeNode(treeNode, giftTypeList);
            res.add(treeNode);
        }
        return res;
    }
    public void getChildTreeNode(TreeNode treeNode, List<GiftType> giftTypeList) {
        String idStr = String.valueOf(treeNode.getId());
        List<GiftType> childGiftTypeList = giftTypeList.stream().filter(x-> x.getPid().toString().equals(idStr)).collect(Collectors.toList());
        for(GiftType childNode : childGiftTypeList){
            TreeNode childTreeNode = new TreeNode(childNode.getId().toString(), childNode.getName());
            treeNode.addNode(childTreeNode);
            getChildTreeNode(childTreeNode, giftTypeList);
        }
    }

    @Override
    public void deleteGiftType(int id) {
        QueryWrapper<GiftType> qw = new QueryWrapper<>();
        qw.eq("pid", id);
        Long childCount = baseMapper.selectCount(qw);
        if(childCount > 0){
            throw new ServiceException("礼品分类有下级，不能删除");
        }
        QueryWrapper<Gift> qw2 = new QueryWrapper<>();
        qw.eq("gift_type", id);
        Long giftCount = giftMapper.selectCount(qw2);
        if(giftCount > 0){
            throw new ServiceException("礼品分类已有商品，不能删除");
        }
        baseMapper.deleteById(id);
    }
}
